import type {
  BubbleListItemProps,
  BubbleListProps,
} from 'vue-element-plus-x/types/BubbleList';

import type { MsgInfo } from '#/api/agent/model/conversation';

import { defineStore } from 'pinia';

import { BubbleListItemHandle } from '#/views/customer/chat/components/bubbleConfig';

export type listType = {
  id: string;
  image?: string[];
  key: number;
  readedAt: number;
  role: 'ai' | 'user';
  status: number;
  time: string;
  trans: string;
} & BubbleListItemProps;

interface AgentChatState {
  customerId: Ref<number | string>;
  hasMoreData: Ref<boolean>;
  customerName: Ref<string>;
  agentId: Ref<number>;
  noReadCount: Ref<number>;
  customerList: Ref<any[]>; // 应该替换为具体类型
  agentList: Ref<any[]>; // 应该替换为具体类型
  /** 会话历史数据 */
  conversationHistory: Ref<BubbleListProps<listType>['list']>;
}

interface AgentChatActions {
  setCustomerId: (id: number | string) => void;
  setAgentId: (id: number) => void;
  setCustomerList: (list: listType[]) => void;
  setAgentList: (list: listType[]) => void;
  /** 设置历史数据 */
  setConversationList: (list: listType[]) => void;
  pushConversationList: (obj: MsgInfo[]) => void;
  clearCustomerId: () => void;
  $reset: () => void;
  pushHistoryConversation: (data: MsgInfo[]) => void;
  setHasMoreData: (data: boolean) => void;
  setLeftMsgReadStatus: (data: MsgInfo[]) => void;
  setCustomerName: (data: string) => void;
  updateMsgStatus: (data: MsgInfo[]) => void;
  setNoReadCount: () => void;
  resetNoReadCount: () => void;
  // msgInfoTop: () => void;
}

/**
 * 客服聊天数据管理
 */
export const useAgentChatStore = defineStore(
  'agent-chat',
  (): AgentChatActions & AgentChatState => {
    const customerId = ref(); // 客户ID
    const agentId = ref(1); // 客服ID
    const hasMoreData = ref<boolean>(false); // 是否加载更多数据
    const customerList = ref<any[]>([]); // 客户列表
    const agentList = ref<any[]>([]); // 客服列表
    const customerName = ref<string>(''); // 客户名称
    const conversationHistory = ref<listType[]>([]); // 会话数据
    const noReadCount = ref(0); // 未读消息数量

    // 设置客户id
    function setCustomerId(id: number | string) {
      customerId.value = id;
    }

    // 设置客服id
    function setAgentId(id: number) {
      agentId.value = id;
    }

    // 加载更多会话数据
    function setHasMoreData(data: boolean) {
      hasMoreData.value = data;
    }

    // 清空客户id
    function clearCustomerId() {
      customerId.value = '';
    }

    // 设置客户列表
    function setCustomerList(list: listType[]) {
      customerList.value = list;
    }

    // 设置客服列表
    function setAgentList(list: listType[]) {
      agentList.value = list;
    }

    // 设置会话列表
    function setConversationList(list: listType[]) {
      conversationHistory.value = list;
    }

    // 设置客户信息
    function setCustomerName(data: string) {
      customerName.value = data;
    }

    // 聊天会话推送
    function pushConversationList(msgInfo: MsgInfo[]) {
      const newdata = BubbleListItemHandle(msgInfo);
      conversationHistory.value = [...conversationHistory.value, ...newdata];
    }

    // 设置左侧已读消息状态
    function setLeftMsgReadStatus(data: MsgInfo[]) {
      conversationHistory.value.forEach((item) => {
        data.forEach((ele: MsgInfo) => {
          if (String(ele.id) === String(item.id)) {
            item.readedAt = ele.readedAt;
            item.status = ele.status;
          }
        });
      });
    }

    // 更新消息状态
    function updateMsgStatus(data: MsgInfo[]) {
      const ids = new Set(data.map((item) => item.id));
      conversationHistory.value.forEach((item: any) => {
        if (ids.has(item.id)) {
          item.status = data.find((ele) => ele.id === item.id)?.status || 0;
        }
      });
    }

    // 未读消息数量递增
    function setNoReadCount() {
      noReadCount.value++;
    }

    // 重置未读数量
    function resetNoReadCount() {
      noReadCount.value = 0;
    }

    /** 退出登录重置数据 */
    function $reset() {
      customerId.value = '';
      agentId.value = 1;
      hasMoreData.value = false;
      customerList.value = [];
      agentList.value = [];
      customerName.value = '';
      noReadCount.value = 0;
      conversationHistory.value = [];
    }

    // 历史会话推送
    function pushHistoryConversation(data: MsgInfo[]) {
      // 数据处理

      const newdata = BubbleListItemHandle(data);
      conversationHistory.value = [
        ...newdata,
        ...conversationHistory.value,
      ] as BubbleListProps<listType>['list'];
    }

    return {
      customerId,
      customerName,
      setLeftMsgReadStatus,
      agentId,
      setCustomerId,
      clearCustomerId,
      setAgentId,
      customerList,
      setCustomerName,
      agentList,
      setCustomerList,
      hasMoreData,
      $reset,
      setAgentList,
      updateMsgStatus,
      conversationHistory,
      setConversationList,
      noReadCount,
      setNoReadCount,
      resetNoReadCount,
      pushHistoryConversation,
      pushConversationList,
      // msgInfoTop,
      setHasMoreData,
    };
  },
);
